use std::ffi::CString;

use base_data::channel::Channel;
use device::start_cti_by_event_callback;
use manager::{CallBackFun, Manager, MANAGER};
use tokio::sync::mpsc;
use tools::global_defin::tokio_runtime::get_rt;
use tracing::{error, info};

pub mod context;
pub mod device;
///做为启动后板卡处理的处口
pub mod manager;

///启动三汇板卡的操作..此方法仅能使用一次...
/// to_business: 发送至事件处理逻辑的发送者
pub fn init_synway<F>(f: F)
where
	F: Fn(Channel) + Send + Sync + 'static,
{
	let sh_cfg_file = CString::new("./config/ShConfig.ini").expect("CString::new failed");
	let index_cfg_file = CString::new("./config/ShIndex.ini").expect("index_cfg_file failed");

	let result_code = start_cti_by_event_callback(sh_cfg_file, index_cfg_file, CallBackFun);

	info!("synway板卡初始化完成,注册事件回调函数");
	if result_code == 0 {
		let count = device::count();
		info!("SsmStartCti成功..总通道数:{}", count);

		let (handler_to_switch, from_handler) = mpsc::channel(65535);
		let (device_to_switch, from_device) = mpsc::channel(65535);
		let manager =
			MANAGER.get_or_init(|| Manager::new(count, Box::new(f), handler_to_switch, device_to_switch));
		get_rt().spawn(manager.running(from_handler, from_device));
	} else {
		error!("SsmStartCti失败");
	}
}
